package com.xjoyt.pay.master.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import com.xjoyt.pay.abstracts.AbstractDao;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.master.dao.DistributorPreferenceRepositoryCustom;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.master.po.DistributorPreference;
import com.xjoyt.pay.master.repository.DistributorPreferenceRepository;

public class DistributorPreferenceRepositoryImpl extends AbstractDao implements DistributorPreferenceRepositoryCustom {

	@Autowired
	private DistributorPreferenceRepository repository;

	@Override
	public DistributorPreference findOneByNo(String no) {
		Page<DistributorPreference> page = repository.findAllByNo(no, getFirstPageable());
		return page.hasContent() ? page.iterator().next() : null;
	}

	@Override
	public ExecuteResult<?> save(Distributor distributor) {
		DistributorPreference entity = new DistributorPreference();
		entity.distributor = distributor;
		entity.id = distributor.id;
		return create(entity);
	}

	@Override
	public ExecuteResult<?> create(DistributorPreference entity) {
		// if (StringUtils.isBlank(entity.token))
		// entity.token = UUID.randomUUID().toString().substring(0,
		// 6).toUpperCase();
		// if (StringUtils.isBlank(entity.signTextName))
		// entity.signTextName = UUID.randomUUID().toString().replaceAll("-",
		// StringUtils.EMPTY).substring(0, 16);
		// if (StringUtils.isBlank(entity.signTextValue))
		// entity.signTextValue = UUID.randomUUID().toString().replaceAll("-",
		// StringUtils.EMPTY);
		repository.save(entity);
		return ExecuteResult.ok();
	}

}
